package com.cdcas.dept.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Date;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class BeanUtil {
    private static final DateTimeFormatter DATE_TIME_FORMATTE = DateTimeFormatter.ofPattern("yyyy-MM--dd HH:mm:ss");
    private static final ZoneId ZONE_ID = ZoneId.systemDefault();

    public static void setObjectValue(Object object,String value) {
        String all[] =IFieldContentSplit.splitAll(value);
        for (String con:all) {
            try {
                String fields[] = IFieldContentSplit.splitField(con);
                if (fields[0].contains(".")) {
                    String cascade[] = fields[0].split("\\.");
                    instanceCasCadeObject(object, cascade);
                } else {
                    Field field1 = object.getClass().getDeclaredField(fields[0]);
                    Method method = object.getClass().getMethod("set" + StringUtil.initcap(fields[0]), field1.getType());
                    method.invoke(object, convert(fields[1], field1));
                }
            } catch (Exception e) {
                    e.printStackTrace();
            }
        }
    }

    public static Object instanceCasCadeObject(Object obj,String cacade[]) throws Exception {
        for (int x=0;x<cacade.length-1;x++) {
            Method getMethod = obj.getClass().getMethod("get"+StringUtil.initcap(cacade[x]));
            Object instance = getMethod.invoke(obj);
            if (instance == null) {
                Field field = obj.getClass().getDeclaredField(cacade[x]);
                Object obj1 = field.getType().getConstructor().newInstance();
                Method setMethod = obj.getClass().getMethod("set"+StringUtil.initcap(cacade[x]),field.getType());
                setMethod.invoke(obj,obj1);
                obj = obj1;
            } else {
                obj = instance;
            }
        }
        return obj;
    }

    public static void setFieldValue(Object object,String fieldName,String fieldValue) throws Exception {
        Field field = object.getClass().getDeclaredField(fieldName);
        Method method = object.getClass().getMethod("set"+StringUtil.initcap(fieldName),field.getType());
        method.invoke(object,convert(fieldValue,field));
    }

    public static Object convert(String value,Field field) {
        String fieldTypeName = field.getType().getName();
        try {
            switch (fieldTypeName) {
                case "java.lang.String":return value;
                case "int":return Integer.parseInt(value);
                case "java.lang.Integer":return Integer.parseInt(value);
                case "double":return Double.parseDouble(value);
                case "java.lang.Double":return Double.parseDouble(value);
                case "java.lang.BigDecimal":return new BigDecimal(value);
                case "long":return Long.parseLong(value);
                case "java.lang.Long":return Long.parseLong(value);
                case "java.util.Date": {
                  LocalDateTime  localDateTime  = LocalDateTime.parse(value,DATE_TIME_FORMATTE);
                    Instant instant = localDateTime.atZone(ZONE_ID).toInstant();
                    return Date.from(instant);
                }
            }
        } catch (Exception e) {
        }
        return null;
    }
}
